package com.example.g32.paging

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.chad.library.adapter.base.BaseQuickAdapter
import com.example.base.entity.NoticeEntityItem
import com.example.g32.R
import com.example.g32.databinding.ItemHomeBinding

class NoticePageAdapter :PagingDataAdapter<NoticeEntityItem,NoticePageAdapter.NoticeViewHolder >(diff){
    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    private var mOnItemClickListener: OnItemClickListener? = null
    object diff : DiffUtil.ItemCallback<NoticeEntityItem>() {
        override fun areItemsTheSame(
            oldItem: NoticeEntityItem,
            newItem: NoticeEntityItem
        ): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(
            oldItem: NoticeEntityItem,
            newItem: NoticeEntityItem
        ): Boolean {
            return oldItem == newItem
        }

    }


    class NoticeViewHolder(view:View):ViewHolder(view){
        val binding = DataBindingUtil.bind<ItemHomeBinding>(view)
    }

    override fun onBindViewHolder(holder: NoticeViewHolder, position: Int) {
        val item = getItem(position)
        holder.binding?.bean = item

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position, item!!)
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemLongClick(position, item!!)

            return@setOnLongClickListener true

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoticeViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_home, parent, false)
        return NoticeViewHolder(inflate)
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }

     fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }
}
interface OnItemLongClickListener {
    fun onItemLongClick( position: Int, bean : NoticeEntityItem): Boolean
}

interface OnItemClickListener {
    fun onItemClick(position: Int, bean : NoticeEntityItem)
}